Getting started with SWS
Prerequisites
First program: Hello world
Write a new class that extends the sws.structure.WebPage and implements a new interface:
public class Sample01HelloWorld extends WebPage implements Sample01HelloWorldFunctionList {
Make the interface contain one method:
public void index();
Implement the method:
@Override
public void index() {
HtmlOutput htmlOutput = HtmlOutput.getHtmlOutput();
htmlOutput.writeTitle("Sample 01 - Hello World - Web page");
htmlOutput.writeHeader("Hello World", 1);
htmlOutput.writeText("If you see this it's working.");
}
Implement a main method:
public static void main(String[] args) {
SimpleWebServer.runServer(8080, new Sample01HelloWorld());
}
The full code:
import sws.html.HtmlOutput;
import sws.server.SimpleWebServer;
import sws.structure.WebPage;
public class Sample01HelloWorld extends WebPage implements Sample01HelloWorldFunctionList {
public static void main(String[] args) {
SimpleWebServer.runServer(8080, new Sample01HelloWorld());
}
@Override
public void index() {
HtmlOutput htmlOutput = HtmlOutput.getHtmlOutput();
htmlOutput.writeTitle("Sample 01 - Hello World - Web page");
htmlOutput.writeHeader("Hello World", 1);
htmlOutput.writeText("If you see this it's working.");
}
}
Run the code. It should print the following in the console:
Server is running on:
http://localhost:8080/
Browse to http://localhost:8080/
It should look as follows:
Hello World
If you see this it's working.
Definitions
Here are a few definitions that will help you to understand the more complex samples to come.
Simple Web Server
A simple web server that can run one or more web applications on one port at the same time.
It also contains general configuration (404 page, 500 page and more) for them.
Web directory
A web directory can contain zero, one or more web directories, web pages or web resources.
Web application
A Web application contains an entire web application with all the web directories, web pages, web resources and scheduled tasks in it and some general configuration (404 page, 500 page and more).
Scheduled tasks
A scheduled task defines what (Web) page function to call and when.
There are two types of scheduled tasks: Repeated (repeatedCall) and non repeated (callDelayed).
Web resource
A web resource is non HTML web resource, e.g. an image, zip, jar and other file types.
Web page
A web page contains (web) page function(s).
(Web) page function
A Java method that can generate HTML and "return" it. This method in also defined in the function list interface.
Function list
A Java interface that defines all the (Web) page function(s) in a web page.
Form object
An object set on a form tag using the htmlOutput.startForm method as default data object for the form
Data object
An object used as parameter to one of the many htmlOutput.write*InputFieldAndAssociateItWith methods to set the object on which the getter and setter should be called.
This will "override" the form object.
Property list
A Java interface that defines all the getters (and thereby setters) that can be use when updating data using the HTML form tag.
Program 2: Simple link
Write a new class that extends the sws.structure.WebPage and implements a new interface:
public class Sample02SimpleLink extends WebPage implements Sample02SimpleLinkFunctionList {
Make the interface contain two methods:
public void index();
public void pageFunction2();
Implement the two methods:
@Override
public void index() {
HtmlOutput htmlOutput = HtmlOutput.getHtmlOutput();
htmlOutput.writeTitle("Sample 02 - Simple link - Web page");
htmlOutput.writeHeader("Simple link", 1);
htmlOutput.writeText("This sample shows how to link to another page (Page function).");
htmlOutput.writeBreakLine();
htmlOutput.writeBreakLine();
htmlOutput.writeLink(Sample02SimpleLinkFunctionList.class, "This is the label of the link").pageFunction2();
htmlOutput.writeBreakLine();
}
@Override
public void pageFunction2() {
HtmlOutput htmlOutput = HtmlOutput.getHtmlOutput();
htmlOutput.writeTitle("Sample 02 - Simple link - Web page - pageFunction2");
htmlOutput.writeHeader("Simple link", 1);
htmlOutput.writeText("Congratulations you have clicked on the link.");
}
Implement a main method:
public static void main(String[] args) {
SimpleWebServer.runServer(8080, new Sample02SimpleLink());
}
For the full listing of the source code see the sws.sample.sample02simplelink package in the source code zip file.
Run the code and browse to http://localhost:8080/
Program 3: Simple object update
Same procedure as above: Write a new class with two methods defined in an interface and one main method:
import sws.html.HtmlOutput;
import sws.server.SimpleWebServer;
import sws.structure.WebPage;
public class Sample03SimpleObjectUpdateWebPage extends WebPage implements Sample03SimpleObjectUpdateWebPageFunctionList {
public static void main(String[] args) {
SimpleWebServer.runServer(8080, new Sample03SimpleObjectUpdateWebPage());
}
private Sample03SimpleObjectUpdateData sample03SimpleObjectUpdateData = new Sample03SimpleObjectUpdateData();
@Override
public void index() {
HtmlOutput htmlOutput = HtmlOutput.getHtmlOutput();
htmlOutput.writeTitle("Sample 03 - Simple object update - Web page");
htmlOutput.writeHeader("Simple object update", 1);
htmlOutput.startCenter();
htmlOutput.startForm(Sample03SimpleObjectUpdateWebPageFunctionList.class, sample03SimpleObjectUpdateData).saveData();
htmlOutput.writeLabelForNextInputField("Text field ");
htmlOutput.writeTextInputFieldAndAssociateItWith(Sample03SimpleObjectUpdateDataPropertyList.class).getTextField();
htmlOutput.writeSubmitButton("Save data");
htmlOutput.endForm();
htmlOutput.endCenter();
}
@Override
public void saveData() {
HtmlOutput htmlOutput = HtmlOutput.getHtmlOutput();
htmlOutput.writeTitle("Sample 03 - Simple object update - Web page");
htmlOutput.writeHeader("Data saved", 1);
htmlOutput.writeText("The text field was updated to contain: \"" + sample03SimpleObjectUpdateData.getTextField() + "\"");
htmlOutput.writeBreakLine();
htmlOutput.writeBreakLine();
htmlOutput.writeLink(Sample03SimpleObjectUpdateWebPageFunctionList.class, "Back").index();
}
}
Make note of the comments before the line:
htmlOutput.startForm(Sample03SimpleObjectUpdateWebPageFunctionList.class, sample03SimpleObjectUpdateData).saveData();
and before this line:
htmlOutput.writeTextInputFieldAndAssociateItWith(Sample03SimpleObjectUpdateDataPropertyList.class).getTextField();
They are important to understand (and a bit weird).
Write a very simple data class with getters and setters that implements an interface that defines the getter:
import java.io.Serializable;
public class Sample03SimpleObjectUpdateData implements Sample03SimpleObjectUpdateDataPropertyList, Serializable {
private static final long serialVersionUID = -4323935484447061760L;
private String textField;
@Override
public String getTextField() {
return textField;
}
public void setTextField(String ptextField) {
textField = ptextField;
}
}
For the full listing of the source code see the sws.sample.sample03simpleobjectupdate package in the source code zip file.
Run the code and browse to http://localhost:8080/
The next programs
See the packages in the sws.sample package in the source code zip file for more sample programs.